"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports[Symbol.toStringTag] = "Module";
const containerClass = "ele-modal-container";
const closedClass = "ele-modal-closed";
const movableClass = "ele-modal-movable";
const moveOutClass = "ele-modal-move-out";
const moveOutPositiveClass = "ele-modal-move-out-positive";
const resizableClass = "ele-modal-resizable";
const resizableHorizontalClass = "ele-modal-resizable-horizontal";
const resizableVerticalClass = "ele-modal-resizable-vertical";
const multipleClass = "ele-modal-multiple";
const fullscreenClass = "ele-modal-wrap-fullscreen";
const innerClass = "ele-modal-inner";
const innerMaskClass = "ele-modal-inner-mask";
const hideClass = "ele-modal-hide";
const centeredClass = "ele-modal-centered";
const collapseClass = "ele-state-collapse";
const navCollapseClass = "ele-state-nav-collapse";
const bodyFullscreenClass = "ele-state-body-fullscreen";
const showTabsClass = "ele-state-show-tabs";
const sideMixClass = "ele-state-side-mix";
const sideMixSingleClass = "ele-state-side-mix-single";
const layoutTopClass = "ele-state-layout-top";
const contentFullscreenClass = "ele-state-content-fullscreen";
const mobileClass = "ele-state-mobile";
const wrapClass = "el-dialog__wrapper";
const modalClass = "el-dialog";
const headerClass = "el-dialog__header";
const titleClass = "el-dialog__title";
const modalDefaultZIndex = 1e3;
const defaultMinWidth = 260;
const defaultMinHeight = 160;
function initModalStyle(modalEl, resizable) {
  modalEl.style.top = modalEl.offsetTop + "px";
  modalEl.style.left = modalEl.offsetLeft + "px";
  modalEl.style.bottom = "auto";
  modalEl.style.right = "auto";
  modalEl.style.margin = "0";
  modalEl.style.position = "relative";
  modalEl.style.display = resizable ? "inline-flex" : "inline-block";
  modalEl.style.verticalAlign = "top";
}
function getCurrentStyle(el) {
  return el["currentStyle"] || window.getComputedStyle(el, null) || {};
}
function getStyleString(obj) {
  const keys = Object.keys(obj);
  if (!keys.length) {
    return "";
  }
  return keys.map((key) => key + ":" + obj[key]).join(";") + ";";
}
function getModalContainer() {
  const elem = document.querySelector("." + containerClass);
  if (elem) {
    return elem;
  }
  const el = document.createElement("div");
  el.classList.add(containerClass);
  document.body.appendChild(el);
  return el;
}
function getVisibleModalWrap() {
  return getModalContainer().querySelectorAll(`.${wrapClass}:not(.${closedClass})`);
}
function getModalMaxIndex(el) {
  const wrapEl = getVisibleModalWrap();
  let maxIndex = 0;
  if (wrapEl) {
    for (let i = 0; i < wrapEl.length; i++) {
      const index = Number(getCurrentStyle(wrapEl[i]).zIndex || modalDefaultZIndex);
      if (index >= maxIndex) {
        if (el) {
          if (wrapEl[i] !== el) {
            maxIndex = index + 1;
          }
        } else {
          maxIndex = index + 1;
        }
      }
    }
  }
  return maxIndex || modalDefaultZIndex;
}
function bindMovableEvent(wrapEl) {
  wrapEl.addEventListener("mousedown", (event) => {
    const wrapClass2 = wrapEl == null ? void 0 : wrapEl.classList;
    if (wrapClass2 == null ? void 0 : wrapClass2.contains(fullscreenClass)) {
      return;
    }
    const moveOut = wrapClass2 == null ? void 0 : wrapClass2.contains(moveOutClass);
    const moveOutPositive = wrapClass2 == null ? void 0 : wrapClass2.contains(moveOutPositiveClass);
    if (!(wrapClass2 == null ? void 0 : wrapClass2.contains(movableClass)) && !moveOut) {
      return;
    }
    const headerEl = wrapEl.querySelector("." + headerClass);
    const titleEl = headerEl.querySelector("." + titleClass);
    if (event.target !== headerEl && event.target !== titleEl) {
      return;
    }
    wrapEl.style.userSelect = "none";
    const dialogEl = wrapEl.querySelector("." + modalClass);
    initModalStyle(dialogEl, wrapClass2 == null ? void 0 : wrapClass2.contains(resizableClass));
    const downX = event.clientX;
    const downY = event.clientY;
    const downOL = dialogEl.offsetLeft;
    const downOT = dialogEl.offsetTop;
    const mousemoveFn = function(e) {
      let l = e.clientX - downX + downOL;
      let t = e.clientY - downY + downOT;
      if (!moveOut) {
        const limitL = wrapEl.clientWidth - dialogEl.clientWidth - 1;
        if (l < 0) {
          l = 0;
        } else if (l > limitL) {
          l = limitL;
        }
        const limitT = wrapEl.clientHeight - dialogEl.clientHeight - 2;
        if (t > limitT) {
          t = limitT;
        }
        if (t < 0) {
          t = 0;
        }
      } else if (moveOutPositive) {
        if (l < 0) {
          l = 0;
        }
        if (t < 0) {
          t = 0;
        }
      }
      dialogEl.style.left = l + "px";
      dialogEl.style.top = t + "px";
    };
    const mouseupFn = function() {
      wrapEl.style.userSelect = "";
      document.removeEventListener("mousemove", mousemoveFn);
      document.removeEventListener("mouseup", mouseupFn);
    };
    document.addEventListener("mousemove", mousemoveFn);
    document.addEventListener("mouseup", mouseupFn);
  });
}
function bindResizableEvent(wrapEl) {
  wrapEl.addEventListener("mousedown", function(event) {
    const wrapClass2 = wrapEl == null ? void 0 : wrapEl.classList;
    if (!(wrapClass2 == null ? void 0 : wrapClass2.contains(resizableClass))) {
      return;
    }
    if (wrapClass2 == null ? void 0 : wrapClass2.contains(fullscreenClass)) {
      return;
    }
    const dialogEl = wrapEl.querySelector("." + modalClass);
    const moveOut = wrapClass2 == null ? void 0 : wrapClass2.contains(moveOutClass);
    const canSetWidth = !(wrapClass2 == null ? void 0 : wrapClass2.contains(resizableVerticalClass));
    const canSetHeight = !(wrapClass2 == null ? void 0 : wrapClass2.contains(resizableHorizontalClass));
    const limitX = dialogEl.clientWidth + dialogEl.offsetLeft - wrapEl.scrollLeft + wrapEl.offsetLeft;
    const limitY = dialogEl.clientHeight + dialogEl.offsetTop - wrapEl.scrollTop + wrapEl.offsetTop;
    if (event.clientX > limitX || limitX - event.clientX > 10 || event.clientY > limitY || limitY - event.clientY > 10) {
      return;
    }
    wrapEl.style.userSelect = "none";
    initModalStyle(dialogEl, true);
    const downX = event.clientX;
    const downY = event.clientY;
    const downW = dialogEl.clientWidth;
    const downH = dialogEl.clientHeight;
    const mousemoveFn = function(e) {
      if (canSetWidth) {
        const w = e.clientX - downX + downW;
        const maxW = wrapEl.clientWidth - dialogEl.offsetLeft - 1;
        const nw = (w < defaultMinWidth ? defaultMinWidth : !moveOut && w > maxW ? maxW : w) + "px";
        dialogEl.style.width = nw;
        dialogEl.style.maxWidth = nw;
        dialogEl.style.minWidth = nw;
      }
      if (canSetHeight) {
        const h = e.clientY - downY + downH;
        const maxH = wrapEl.clientHeight - dialogEl.offsetTop - 1;
        const nh = (h < defaultMinHeight ? defaultMinHeight : !moveOut && h > maxH ? maxH : h) + "px";
        dialogEl.style.height = nh;
        dialogEl.style.maxHeight = nh;
        dialogEl.style.minHeight = nh;
      }
    };
    const mouseupFn = function() {
      wrapEl.style.userSelect = "";
      document.removeEventListener("mousemove", mousemoveFn);
      document.removeEventListener("mouseup", mouseupFn);
    };
    document.addEventListener("mousemove", mousemoveFn);
    document.addEventListener("mouseup", mouseupFn);
  });
}
function bindAutoSetTopEvent(wrapEl) {
  wrapEl.addEventListener("mousedown", () => {
    setModalTop(wrapEl);
  });
}
function setModalTop(wrapEl) {
  var _a;
  if (!((_a = wrapEl == null ? void 0 : wrapEl.classList) == null ? void 0 : _a.contains(multipleClass))) {
    return;
  }
  const zIndex = Number(getCurrentStyle(wrapEl).zIndex || modalDefaultZIndex);
  const maxIndex = getModalMaxIndex(wrapEl);
  if (maxIndex > zIndex) {
    wrapEl.style.zIndex = String(maxIndex);
  }
}
function resetModalStyle(wrapEl, width, top) {
  const modalEl = wrapEl == null ? void 0 : wrapEl.querySelector("." + modalClass);
  if (!modalEl) {
    return;
  }
  modalEl.style.top = "";
  modalEl.style.left = "";
  modalEl.style.bottom = "";
  modalEl.style.right = "";
  modalEl.style.margin = "";
  modalEl.style.marginTop = typeof top === "number" ? top + "px" : top;
  modalEl.style.position = "";
  modalEl.style.display = "";
  modalEl.style.verticalAlign = "";
  modalEl.style.height = "";
  modalEl.style.maxHeight = "";
  modalEl.style.minHeight = "";
  modalEl.style.width = typeof width === "number" ? width + "px" : width;
  modalEl.style.maxWidth = "";
  modalEl.style.minWidth = "";
}
exports.bindAutoSetTopEvent = bindAutoSetTopEvent;
exports.bindMovableEvent = bindMovableEvent;
exports.bindResizableEvent = bindResizableEvent;
exports.bodyFullscreenClass = bodyFullscreenClass;
exports.centeredClass = centeredClass;
exports.closedClass = closedClass;
exports.collapseClass = collapseClass;
exports.containerClass = containerClass;
exports.contentFullscreenClass = contentFullscreenClass;
exports.fullscreenClass = fullscreenClass;
exports.getModalContainer = getModalContainer;
exports.getStyleString = getStyleString;
exports.hideClass = hideClass;
exports.innerClass = innerClass;
exports.innerMaskClass = innerMaskClass;
exports.layoutTopClass = layoutTopClass;
exports.mobileClass = mobileClass;
exports.movableClass = movableClass;
exports.moveOutClass = moveOutClass;
exports.moveOutPositiveClass = moveOutPositiveClass;
exports.multipleClass = multipleClass;
exports.navCollapseClass = navCollapseClass;
exports.resetModalStyle = resetModalStyle;
exports.resizableClass = resizableClass;
exports.resizableHorizontalClass = resizableHorizontalClass;
exports.resizableVerticalClass = resizableVerticalClass;
exports.setModalTop = setModalTop;
exports.showTabsClass = showTabsClass;
exports.sideMixClass = sideMixClass;
exports.sideMixSingleClass = sideMixSingleClass;
